home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / UNIX.ZIP / GUESS / MAIN.C < prev    next >
C/C++ Source or Header  |  1991-11-04  |  7KB  |  359 lines

  1.     /*****************************************************
  2.  
  3.           Fast DES program module
  4.        - Copyright 1991, Christian Beaumont
  5.  
  6.        This program is free software; you can
  7.        redistribute it and/or modify it as you see fit.
  8.  
  9.        This program is distributed in the hope that it
  10.        will be useful, but WITHOUT ANY WARRANTY; without
  11.        even the implied warranty of MERCHANTABILITY or
  12.        FITNESS FOR A PARTICULAR PURPOSE.
  13.  
  14.     *****************************************************/
  15.  
  16. #include <stdio.h>
  17. #include <alloc.h>
  18. #include <bios.h>
  19. #include <string.h>
  20. #include <dos.h>
  21. #include <io.h>
  22. #include "fdes.h"
  23.  
  24. #define PMAX    1300
  25.  
  26. #define VER_MAJ    2
  27. #define VER_MIN 1
  28.  
  29. int     catchup = 0;
  30. char  catchword[40];
  31.  
  32. int    allflag = 1;
  33. char     u_name[40];
  34.  
  35. struct passwd2 *p;
  36. int    n;
  37. long   crypts = 0;
  38.  
  39.  
  40. void S_Check(char *s)
  41. {
  42. int i;
  43.  
  44.     setname(s);
  45.  
  46.     for(i=0; i<n; i++){
  47.         if(p[i].pw_broken == 0){
  48.             if(shortcrypt(p[i].pw_out96,p[i].pw_salt)){
  49.                 printf("%s is %s\n",p[i].pw_user,s);
  50.                 strncpy(p[i].pw_passwd,s,8);
  51.                 p[i].pw_broken = 1;
  52.             }
  53.             crypts++;
  54.         }
  55.     }
  56. }
  57.  
  58. void A_Check(void)
  59. {
  60. char try[20];
  61. int i;
  62.         
  63.     for(i=0; i<n; i++){
  64.         if(p[i].pw_broken == 0){
  65.             strcpy(try,p[i].pw_user);
  66.             try[8] = '\0';
  67.             setname(try);
  68.             if(shortcrypt(p[i].pw_out96,p[i].pw_salt)){
  69.                 printf("%s is %s\n",p[i].pw_user,try);
  70.                 strncpy(p[i].pw_passwd,try,8);
  71.                 p[i].pw_broken = 1;
  72.             }
  73.             crypts++;
  74.         }
  75.     }
  76.  
  77.     for(i=0; i<n; i++){
  78.         if(p[i].pw_broken == 0){
  79.             strcpy(try,p[i].pw_user);
  80.             strcat(try,p[i].pw_user);
  81.             try[8] = '\0';
  82.             setname(try);
  83.             if(shortcrypt(p[i].pw_out96,p[i].pw_salt)){
  84.                 printf("%s is %s\n",p[i].pw_user,try);
  85.                 strncpy(p[i].pw_passwd,try,9);
  86.                 p[i].pw_broken = 1;
  87.             }
  88.             crypts++;
  89.         }
  90.     }
  91. }
  92.  
  93. void U_Check(char *d)
  94. {
  95. FILE *fin;
  96. char    w[40];
  97. char    s[40];
  98. int   i;
  99.  
  100.     if((fin = fopen(d,"r")) == NULL){
  101.         printf("Could not open file %s (continuing)\n",d);
  102.         return;
  103.     }
  104.  
  105.     if(catchup != 0){
  106.         while(fgets(s,39,fin) != NULL){
  107.             *strchr(s,'\n') = '\0';
  108.             if(!strcmp(s,catchword))
  109.                 goto Resume;
  110.         }
  111.         return;
  112.     }
  113.  
  114. Resume:
  115.  
  116.     catchup = 0;
  117.  
  118.     for(i=0;i<n;i++){
  119.         if(!strcmp(p[i].pw_user,u_name))
  120.             break;
  121.     }
  122.  
  123.     if(i == n)
  124.         return;
  125.  
  126.     if(p[i].pw_broken == 1)
  127.         return;
  128.  
  129.     while(fgets(s,39,fin) != NULL){
  130.         if(s[0] == '\n')
  131.             continue;
  132.         strcpy(w,s);
  133.         *strchr(w,'\n') = '\0';
  134.         w[8] = '\0';
  135.         crypts++;
  136.         setname(w);
  137.         if(shortcrypt(p[i].pw_out96,p[i].pw_salt)){
  138.             printf("%s is %s\n",p[i].pw_user,w);
  139.             strcpy(p[i].pw_passwd,w);
  140.             p[i].pw_broken = 1;
  141.             return;
  142.         }
  143.         if(kbhit()){
  144.             printf("Terminated just after checking %s\n",s);
  145.             return;
  146.         }
  147.     }
  148. }
  149.  
  150. void F_Check(char *d)
  151. {
  152. FILE *fin;
  153. char    w[40];
  154. char    s[40];
  155. int   i;
  156.  
  157.     if((fin = fopen(d,"r")) == NULL){
  158.         printf("Could not open file %s (continuing)\n",d);
  159.         return;
  160.     }
  161.  
  162.     if(catchup != 0){
  163.         while(fgets(s,39,fin) != NULL){
  164.             *strchr(s,'\n') = '\0';
  165.             if(!strcmp(s,catchword))
  166.                 goto Resume;
  167.         }
  168.         return;
  169.     }
  170.  
  171. Resume:
  172.  
  173.     catchup = 0;
  174.  
  175.     while(fgets(s,39,fin) != NULL){
  176.         if(s[0] == '\n')
  177.             continue;
  178.         strcpy(w,s);
  179.         *strchr(w,'\n') = '\0';
  180.         w[8] = '\0';
  181.         setname(w);
  182.         for(i=0; i<n; i++){
  183.  
  184.             if(shortcrypt(p[i].pw_out96,p[i].pw_salt)){
  185.                 printf("%s is %s\n",p[i].pw_user,w);
  186.                 strcpy(p[i].pw_passwd,w);
  187.                 p[i].pw_broken = 1;
  188.             }
  189.             crypts++;
  190.         }
  191.         if(kbhit()){
  192.             printf("Terminated just after checking %s\n",s);
  193.             return;
  194.         }
  195.     }
  196. }
  197.  
  198. main(int argc,char *argv[])
  199. {
  200. long    time;
  201. int    i,update;
  202. struct passwd *p1;
  203. FILE *fout;
  204. int got=0;
  205.  
  206.     if(argc < 2){
  207.           printf("\nGUESS: Unix Password Checker [Version %d.%d]\n",
  208.                 VER_MAJ,VER_MIN);
  209.  
  210.         puts("GUESS - Copyright 1991 Christian Beaumont\n"
  211.               "C_BEAUMONT@UK.AC.LUT.HICOM and Sysop of fidonet node 2:250/301\n"
  212.               "THIS IS A BETA TEST VERSION FOR PUBLIC DISTRIBUTION\n"
  213.               "Use: GUESS [PASSWORD_FILE] [[OPTION] [DICTIONARY]] ...\n"
  214.               "Options are:\n\n"
  215.               "      -a       Perform algorithmic check of password file\n"
  216.               "      -cWORD   Catch up to specified word in dictionary\n"
  217.               "      -sWORD   Check password file against specifed word only\n"
  218.               "      -uNAME   Limit checking of dictionary to a certain user name\n"
  219.               "      -w       Permit checking of dictionary to all users\n\n"
  220.               "If you find GUESS useful or have any comments please feel free\n"
  221.               "to contact me.\n\n"
  222.               "Christian Beaumont +44-81-883-5683 (home) +44-81-680-1066 (work)\n"
  223.               );
  224.         return 1;
  225.     }
  226.  
  227.     printf("Reading password file ... ");
  228.  
  229.     for(n=0;getpwent(argv[1])!=NULL;n++);
  230.  
  231.     printf("\nENTRIES  %d\n",n);
  232.  
  233.     if(n==0)
  234.         return 1;
  235.  
  236.     if((p = calloc(n,sizeof(struct passwd2))) == NULL){
  237.         printf("Couldn't allocate space for %d entries",PMAX);
  238.         return 1;
  239.     }
  240.  
  241.     n = 0;
  242.     while((p1 = getpwent(argv[1])) != NULL){
  243.         if(p1->pw_passwd[0] == '='){
  244.             got++;
  245.             continue;
  246.         }
  247.         if(strlen(p1->pw_passwd) != 13)
  248.             continue;
  249.         if(strchr(p1->pw_passwd,'*') != NULL)
  250.             continue;
  251.         strcpy(p[n].pw_user,p1->pw_user);
  252.         p[n].pw_salt = flatten(p1->pw_passwd,p[n].pw_out96);
  253.         p[n].pw_broken = 0;
  254.         p[n].pw_passwd[0] = '\0';
  255.         n++;
  256.     }
  257.  
  258.     if(n == 0){
  259.         puts("NO ENTRIES READ");
  260.         return 1;
  261.     }
  262.  
  263.     if(n == PMAX)
  264.         printf("INSUFFICIENT MEMORY TO READ ALL ENTRIES! (CONTINUING)");
  265.  
  266.     printf("BROKEN   %d\n",got);
  267.     printf("UNBROKEN %d\n",n);
  268.     printf("PERCENT BROKEN  %d\n",(100 * got)/(got + n));
  269.  
  270.     puts("Initialising tables");
  271.     init_des();
  272.  
  273.    time = biostime(0,0);
  274.  
  275.     for(i=2; i<argc; i++){
  276.         if(argv[i][0] != '-'){
  277.             printf("Checking against file %s\n",argv[i]);
  278.             if(allflag == 1)
  279.                 F_Check(argv[i]);
  280.             else
  281.                 U_Check(argv[i]);
  282.         } else{
  283.             switch(argv[i][1]){
  284.                 case 'c' :
  285.                     strcpy(catchword,&argv[i][2]);
  286.                     puts(catchword);
  287.                     catchup = 1;
  288.                     break;
  289.                 case 'a' :
  290.                     puts("Performing algorithmic check");
  291.                     A_Check();
  292.                     break;
  293.                 case 'u'    :
  294.                     strcpy(u_name,&argv[i][2]);
  295.                     allflag = 0;
  296.                     break;
  297.                 case 'w' :
  298.                     allflag = 1;
  299.                     break;
  300.                 case 's'    :
  301.                     S_Check(&argv[i][2]);
  302.                     break;
  303.                 default  :
  304.                     printf("Unknown option %c\n",argv[i][1]);
  305.             }
  306.         }
  307.     }
  308.  
  309.    time = biostime(0,0)-time;
  310.     time++;
  311.  
  312.    printf("%ld guesses in %d seconds (%d guesses/sec %ld clocks)\n",crypts,
  313.                                                         (int)((time*10)/182),
  314.                                                         (int)((crypts*182)/(time*10)),time);
  315.  
  316.     if((fout = fopen("$$$.pwd","w")) == NULL){
  317.         printf("Couldn't open file %s\n",argv[1]);
  318.         free(p);
  319.         return 1;
  320.     }
  321.  
  322.     update = 0;
  323.     for(i=0;i<n;i++)
  324.         if(p[i].pw_broken)
  325.             update++;
  326.  
  327.     if(update == 0){
  328.         free(p);
  329.         return 0;
  330.     }
  331.  
  332.     i = 0;
  333.     while((p1 = getpwent(argv[1])) != NULL){
  334.         if(i<n){
  335.             if(!strcmp(p1->pw_user,p[i].pw_user)){
  336.                 if(p[i].pw_broken){
  337.                     strcpy(p1->pw_passwd,"=");
  338.                     strcat(p1->pw_passwd,p[i].pw_passwd);
  339.                 }
  340.                 i++;
  341.             }
  342.         }
  343.         fprintf(fout,"%s:%s:%d:%d:%s:%s:%s\n",p1->pw_user,p1->pw_passwd,
  344.             p1->pw_uid,p1->pw_gid,p1->pw_gecos,p1->pw_dir,p1->pw_shell);
  345.     }
  346.  
  347.     fclose(fout);
  348.  
  349.     free(p);
  350.  
  351.     unlink(argv[1]);
  352.     rename("$$$.pwd",argv[1]);
  353.  
  354.     return 0;
  355. }
  356.  
  357.  
  358.  
  359.